package com.trip.driver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trip.common.entity.DriverScore;
import com.trip.common.entity.TravelStatus;
import com.trip.common.vo.Result;
import com.trip.driver.mapper.CarInfoMapper;
import com.trip.driver.mapper.TravelStatusMapper;
import com.trip.driver.service.CarInfoService;
import com.trip.driver.service.DriverScoreService;
import com.trip.driver.service.TravelStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.trip.common.vo.DataDictionary.FAIL;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hr
 * @since 2024-09-05
 */
@Service
public class TravelStatusServiceImpl extends ServiceImpl<TravelStatusMapper, TravelStatus> implements TravelStatusService {

    @Autowired
    private DriverScoreService driverScoreService;

    @Override
    public Result updateDriverOrderStatus(TravelStatus travelStatus) {
        if(BeanUtil.isEmpty(travelStatus)) return Result.error(FAIL,"bean is null");
        if( BeanUtil.isEmpty(travelStatus.getDriverId()) || (BeanUtil.isEmpty(travelStatus.getCarId()) && BeanUtil.isEmpty(travelStatus.getStatus()))){
            return Result.error(FAIL,"bean is null");
        }

        //更新接单状态和使用车辆
        LambdaUpdateChainWrapper<TravelStatus> wrapper = this.lambdaUpdate()
                .set(TravelStatus::getStatus, travelStatus.getStatus())
                .set(TravelStatus::getCarId, travelStatus.getCarId())
                .eq(TravelStatus::getDriverId, travelStatus.getDriverId())
                .eq(TravelStatus::getDelFlag,0);
        boolean update = this.update(wrapper);
        return update ? Result.ok("Modification successful"):Result.error(FAIL,"bean is null");
    }

    @Override
    public Result updateDriverScore(DriverScore driverScore) {
        if(BeanUtil.isEmpty(driverScore)) return Result.error(FAIL,"bean is null");
        if( BeanUtil.isEmpty(driverScore.getDriverId()) ){
            return Result.error(FAIL,"driverId is null");
        }

        //更新司机分数
        LambdaUpdateChainWrapper<DriverScore> wrapper = driverScoreService.lambdaUpdate()
                .set(DriverScore::getDriverScore, driverScore.getDriverScore())
                .eq(DriverScore::getDriverId,driverScore.getDriverId())
                .eq(DriverScore::getDelFlag, 0);
        boolean update = driverScoreService.update(wrapper);
        return update ? Result.ok("Modification successful"):Result.error(FAIL,"bean is null");
    }
}
